﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc.Html;
using TjZheng.Framework;
using System.Web.Mvc;

namespace TjZheng.EasyUI
{
    public static partial class EasyUIExtension
    {
        /// <summary>
        /// EasyUI文本框
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="easyClassType">文本框类型（样式）</param>
        /// <param name="name">input的name属性</param>
        /// <param name="value">input的value属性</param>
        /// <param name="isRequired">是否必输</param>
        /// <param name="missingMessage">空值提示信息</param>
        /// <param name="htmlAttributes">html属性</param>
        /// <param name="validTypes">EasyUI提供的验证类型</param>
        /// <returns></returns>
        public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string easyClassType, string name, object value, bool isRequired, string missingMessage, object htmlAttributes, params string[] validTypes)
        {
            IDictionary<string, object> dataOptions = new Dictionary<string, object>();
            dataOptions["required"] = isRequired.ToString().ToLower();
            if (validTypes.IsNoNull())
            {
                dataOptions["validType"] = string.Format("[{0}]", validTypes.JoinComma());
            }
            if (missingMessage.IsNoNull())
            {
                dataOptions["missingMessage"] = string.Format("'{0}'", missingMessage);
            }
            List<string> dataoptionsList = dataOptions.Select(keyValue => keyValue.Key.AppendFormat(":{0}", keyValue.Value)).ToList();
            object data = null;
            IDictionary<string, object> objHtmlAttributes = htmlAttributes.CreateValueDictionary();
            if (objHtmlAttributes.IsNull())
            {
                objHtmlAttributes = new Dictionary<string, object>();
            }
            if (objHtmlAttributes.TryGetValue("dataoptions", out data))
            {
                if (data.ToString().IsNoNull())
                {
                    dataoptionsList.Add(data.ToString());
                }
                objHtmlAttributes.Remove("dataoptions");
            }
            objHtmlAttributes["data-options"] = dataoptionsList.JoinString(",");
            string htmlClass = string.Empty;
            if (easyClassType.IsNoNull())
            {
                htmlClass = easyClassType;
            }
            if (objHtmlAttributes.TryGetValue("class", out data))
            {
                htmlClass = htmlClass.AppendFormat(" {0}", data);
            }
            objHtmlAttributes["class"] = htmlClass;
            return htmlHelper.TextBox(name, value, objHtmlAttributes);
        }
    }
}
